package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.heima.article.dto.ArticleStreamMessage;
import com.heima.article.service.IHotArticleService;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

@Service
public class HotArticleResultListener {


    @Autowired
    private IHotArticleService hotArticleService;

    @KafkaListener(topics = "hot_article_result_topic")
    public void handleMessage(ConsumerRecord<String,String> record){
        System.out.println(record.value());
        // 将value 转换成对象  {"articleId":1503626290400337922,"collect":0,"comment":0,"like":1,"view":1}
        ArticleStreamMessage message = JSON.parseObject(record.value(), ArticleStreamMessage.class);
        // 开始对结果数据处理
        hotArticleService.updateArticle(message);
    }
}
